local skel = fk.CreateSkill {
  name = "rmt__ceju",
  dynamic_desc = function (self, player, lang)
    local name = player:getMark(self.name)
    if name ~= 0 then
      name = name == "slash" and "jink" or "slash"
      return "rmt__ceju_dyn:" .. name
    end
  end,
}

Fk:loadTranslationTable{
  ["rmt__ceju"] = "策局",
  [":rmt__ceju"] = "你可以弃置场上一张牌来视为使用一张【杀】或【闪】。若弃置的牌属于其他角色，你下次发动时须选择一项：优先弃置自己区域的牌；只能发动与上次不同的一项。然后重铸至多两张牌。",

  [":rmt__ceju_dyn"] = "你可以弃置场上一张牌来视为使用一张【杀】或【闪】。若弃置的牌属于其他角色，你下次发动时须选择一项：优先弃置自己区域的牌；只能发动与上次不同的一项。然后重铸至多两张牌（上次：<font color=\"#E0DB2F\">【{1}】</font>）。",

  ["#rmt__ceju"] = "策局：你可以视为使用【杀】或【闪】，弃置场上一张牌",
  ["#rmt__ceju-single"] = "策局：你可弃场上一张牌以使用【杀】或【闪】，若为【%arg】你仅能弃置自己",
  ["#rmt__ceju-choose"] = "策局：弃置场上一张装备牌或锦囊牌，本次使用：【%arg】",
  ["#rmt__ceju-recast"] = "策局:重铸至多两张牌",
}

skel:addEffect("viewas", {
  pattern = "slash,jink",
  prompt = function (self, player, selected_cards, selected)
    if player:getMark(skel.name) == 0 then
      return "#rmt__ceju"
    else
      return "#rmt__ceju-single:::" .. player:getMark(skel.name)
    end
  end,
  interaction = function (self, player)
    local all_names = {"slash", "jink"}
    if #player:getCardIds("ej") == 0 then
      table.removeOne(all_names, player:getMark(skel.name))
    end
    local names = player:getViewAsCardNames(skel.name, all_names)
    if #names > 0 then
      return UI.CardNameBox {choices = names, all_choices = all_names}
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local mark = player:getMark(skel.name)
    local targets = table.filter(room.alive_players, function (p)
      return #p:getCardIds("ej") > 0
    end)
    if #targets == 0 then return skel.name end
    local mustMe = use.card.name == mark
    local to
    if mustMe then
      to = player
    else
      to = room:askToChoosePlayers(player, {
        min_num = 1, max_num = 1, skill_name = skel.name, targets = targets, cancelable = false,
        prompt = "#rmt__ceju-choose:::"..use.card.name,
      })[1]
    end
    if not to or to:getCardIds("ej") == 0 then return skel.name end
    room:setPlayerMark(player, skel.name, 0)
    if to ~= player then
      room:setPlayerMark(player, skel.name, use.card.name)
    end
    local card = #to:getCardIds("ej") == 1 and to:getCardIds("ej") or
    room:askToChooseCard(player, { target = to, flag = "ej", skill_name = skel.name })
    room:throwCard(card, skel.name, to, player)
    if mark ~= 0 then
      use.extra_data = use.extra_data or {}
      use.extra_data.rmt__ceju_recast = true
    end
  end,
  after_use = function (self, player, use)
    if player.dead or player:isNude() then return end
    if use.extra_data and use.extra_data.rmt__ceju_recast then
      local room = player.room
      local cards = room:askToCards(player, {
        min_num = 1, max_num = 2, include_equip = true, skill_name = skel.name, cancelable = true,
        prompt = "#rmt__ceju-recast"
      })
      if #cards > 0 then
        room:recastCard(cards, player, skel.name)
      end
    end
  end,
  enabled_at_play = function (self, player)
    if player:getMark(skel.name) == "slash" and #player:getCardIds("ej") == 0 then return end
    return table.find(Fk:currentRoom().alive_players, function (p)
      return #p:getCardIds("ej") > 0
    end)
  end,
  enabled_at_response = function (self, player, response)
    if response or Fk.currentResponsePattern == nil then return end
    if table.find(Fk:currentRoom().alive_players, function (p)
      return #p:getCardIds("ej") > 0
    end) then
      for _, name in ipairs({"slash", "jink"}) do
        if player:getMark(skel.name) ~= name or #player:getCardIds("ej") > 0 then
          local card = Fk:cloneCard(name)
          if Exppattern:Parse(Fk.currentResponsePattern):match(card) then
            return true
          end
        end
      end
    end
  end,
})



return skel
